package com.hou.credentialscamera;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.hou.Constant;
import com.hou.camera.CardCamera;
import com.hou.utils.FileUtils;

public class MainActivity extends AppCompatActivity {
    private ImageView mIvFront;
    private ImageView mIvBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mIvFront = findViewById(R.id.iv_front);
        mIvBack = findViewById(R.id.iv_back);
    }

    /**
     * 身份证正面
     */
    public void front(View view) {
        CardCamera.init().setActivity(this)
                .setIdCardDirection(CardCamera.TYPE_IDCARD_FRONT)
                .setIdCameraId(Constant.CAMERA_BACK)
                .setCropMode(true)
                .build();
    }

    /**
     * 身份证反面
     */
    public void back(View view) {
        CardCamera.init().setActivity(this)
                .setIdCardDirection(CardCamera.TYPE_IDCARD_BACK)
                .setIdCameraId(Constant.CAMERA_BACK)
                .setCropMode(true)
                .build();
    }

    /**
     * 身份证手持
     */
    public void hand(View view) {
        CardCamera.init().setActivity(this)
                .setIdCardDirection(CardCamera.TYPE_IDCARD_HAND)
                .setIdCameraId(Constant.CAMERA_BACK)
                .setCropMode(true)
                .build();
    }

    /**
     * 其他证件拍摄
     */
    public void other(View view) {
        CardCamera.init().setActivity(this)
                .setIdCardDirection(CardCamera.TYPE_OTHER)
                .setIdCameraId(Constant.CAMERA_BACK)
                .setCropMode(true)
                .build();
    }

    /**
     * 普通拍摄
     */
    public void normal(View view) {
        CardCamera.init().setActivity(this)
                .setIdCardDirection(CardCamera.TYPE_NORMAL)
                .setIdCameraId(Constant.CAMERA_FRONT)
                .setCropMode(false)
                .build();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == CardCamera.RESULT_CODE) {
            //获取图片路径，显示图片
            final String path = CardCamera.getImagePath(data);
            if (!TextUtils.isEmpty(path)) {
                if (requestCode == CardCamera.TYPE_IDCARD_FRONT) { //身份证正面
                    mIvFront.setImageBitmap(BitmapFactory.decodeFile(path));
                } else if (requestCode == CardCamera.TYPE_IDCARD_BACK) {  //身份证反面
                    mIvBack.setImageBitmap(BitmapFactory.decodeFile(path));
                } else if (requestCode == CardCamera.TYPE_NORMAL) {
                    mIvBack.setImageBitmap(BitmapFactory.decodeFile(path));
                }

            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //实际开发中将图片上传到服务器成功后需要删除全部缓存图片
        FileUtils.clearCache(this);
    }
}